package com.jd.system.controller;


import com.jd.basic.util.AjaxsResult;
import com.jd.basic.util.PageList;
import com.jd.system.domain.Dictionarytype;
import com.jd.system.query.TypeQuery;
import com.jd.system.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典",description = "数据字典")
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;


    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "单个查询")
    @GetMapping("/{id}")
    public Dictionarytype findOne(@PathVariable("id")Long id){
        return typeService.loadById(id);
    }


    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Dictionarytype> findAll(){
        return typeService.loadAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxsResult remove(@PathVariable("id")Long id){
        try {
            typeService.remove(id);
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"删除失败");
        }
    }

    /**
     * 修改和添加
     */

    @ApiOperation(value = "修改和添加")
    @PutMapping
    public AjaxsResult save(@RequestBody Dictionarytype dictionarytype){
        try {
            if (dictionarytype.getId()==null){
                // 添加
                typeService.save(dictionarytype);

            }else {
                // 修改
                typeService.update(dictionarytype);
            }
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"操作失败");
        }
    }

    /**
     * 分页查询
     * PageList<Department>返回参数：需要数据和总条数
     *@PathVariable DepartmentQuery departmentQuery：获取路径的当前页和显示的条数
     */

    @ApiOperation(value = "分页查询")
    @PostMapping // 匹配前端url地址
    public PageList<Dictionarytype> queryPage(@RequestBody TypeQuery typeQuery){
        return typeService.queryPage(typeQuery);
    }

    // 批量删除
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxsResult patchRemove(@RequestBody List<Long> ids){
        try {
            typeService.patchRemove(ids);
            return new AjaxsResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"删除失败");
        }
    }

}
